package com.aaa.ps.config;

import com.aaa.common.util.MyCustomCache;
import com.aaa.common.util.RedisProperty;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPoolConfig;

/**
 * fileName:RedisConfig
 * description:
 * author:zz
 * createTime:2020/7/29 16:58
 * version:1.0.0
 */
@Configuration //相当于  <beans></beans>
@ComponentScan("com.aaa.common.util")
public class RedisConfig {

    @Autowired
    private RedisProperty redisProperty;
    /**
     * 配置JedisConnectionFactory 工厂类bean
     * @return
     */
    @Bean  //<bean id/name='jedisConnectionFactory' class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory'>
            //  <constructor-arg  index=0 ref='redisClusterConfiguration'>
            //  <constructor-arg  index=1 ref='jedisPoolConfig'>
           //</bean>
    public JedisConnectionFactory jedisConnectionFactory(){
        ///RedisClusterConfiguration clusterConfig, JedisPoolConfig poolConfig
        //构造注入
        JedisConnectionFactory jedisConnectionFactory =new JedisConnectionFactory(redisClusterConfiguration(),jedisPoolConfig());
        return jedisConnectionFactory;
    }

    /**
     * 加上@Bean后，该方法会被执行，执行时，
     * 调用MyCustomCache中静态方法把上面初始化好的JedisConnectionFactory赋值给MyCustomCache的内部属性
     */
    @Bean
    public void setJedisConnectionFactory(){
        //静态方法注入
        MyCustomCache.setJedisConnectionFactory(jedisConnectionFactory());
    }

    /**
     * 初始化集群的配置
     * @return
     */
    @Bean  //<bean id=redisClusterConfiguration class= org.springframework.data.redis.connection.RedisClusterConfiguration>
    public RedisClusterConfiguration redisClusterConfiguration(){
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        String nodes = redisProperty.getNodes();//nodes=192.168.23.231:6001,192.168.23.231:6002,192.168.23.232:6003,192.168.23.232:6004,192.168.23.233:6005,192.168.23.233:6006
        String[] nodeArray = nodes.split(","); //[192.168.23.231:6001,192.168.23.231:6002,192.168.23.232:6003,192.168.23.232:6004,192.168.23.233:6005,192.168.23.233:6006]
        for (String node : nodeArray) {
            //node =  第一次：192.168.23.231:6001 第二次： 192.168.23.231:6002  ...
            String[] hostAndPort = node.split(":");
            RedisNode redisNode = new RedisNode(hostAndPort[0],Integer.valueOf(hostAndPort[1]));
            redisClusterConfiguration.addClusterNode(redisNode);
        }
        return redisClusterConfiguration;
    }

    /**
     * 初始化链接池配置
     * @return
     */
    @Bean //<bean id='jedisPoolConfig' class="redis.clients.jedis.JedisPoolConfig">
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisProperty.getMaxTotal());
        jedisPoolConfig.setTestOnBorrow(redisProperty.isTestOnBorrow());
        jedisPoolConfig.setMaxWaitMillis(redisProperty.getMaxWaitMillis());
        jedisPoolConfig.setMaxIdle(redisProperty.getMaxIdle());
        return jedisPoolConfig;
    }
}
